package com.example.demo.leetcode.top100;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _79跳跃游戏II
 * @description
 * @date 2023/06/28 17:11
 * <p>
 * ******************************************************
 */
public class _79跳跃游戏II {

    public int jump(int[] nums) {

        int length = nums.length;

        boolean[] dp = new boolean[length];
        int[] res = new int[length];
        dp[0] = true;
        for (int i = 0; i < length; i++) {
            if (dp[i]) {
                for (int j = 1; j <= nums[i] && (i + j) < length; j++) {
                    if (dp[i + j]) {
                        res[i + j] = Math.min(res[i] + 1, res[i + j]);
                    } else {
                        dp[i + j] = true;
                        res[i + j] = res[i] + 1;
                    }
                }
            }
        }
        return res[length-1];
    }
}
